<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: sap<songaiping@hrbjjhh.com>
// +----------------------------------------------------------------------------

namespace Admin\Model;
/**
 * @todo 线上家博会模型
 * @author sap
 * @since 2016-3-9
 */
class OnlineExpoModel{
	
	public $error;//记录最后一次错误信息

	/**
	 * @todo 添加展馆品类
	 * @author sap
	 * @since 2016-3-10
	 */
	public function zoneCategoryAdd(){
	    $activity_id = (int)I('activity_id');
	    $online_category_id = (int)I('online_category_id');
	    $zone_id = (int)I('zone_id');
	    //基础判断
	    if($activity_id == 0){
	        $this->error = '请选择活动';
	        return false;
	    }
	    if($online_category_id == 0){
	        $this->error = '请选择品类';
	        return false;
	    }
	    if($zone_id == 0){
            $this->error = '请选择展馆';
            return false;
	    }
	    //判断是否存在
	    if(empty(getActivityName($activity_id))){
	        $this->error = '活动不存在';
	        return false;
	    }
	    if(empty(getOnlineZoneName($zone_id))){
	        $this->error = '展馆不存在';
	        return false;
	    }
	    if(empty(getOnlineCategoryName($online_category_id))){
	        $this->error = '品类不存在';
	        return false;
	    }
	    $data['activity_id'] = $activity_id;
	    $data['online_category_id'] = $online_category_id;
	    $obj = M('OnlineZoneCategory');
	    if($obj->where($data)->find()){
	        $this->error = '该活动下该品类已经绑定过展馆';
	        return false;
	    }
	    //添加数据
	    $data['zone_id'] = $zone_id;
	    if($obj->add($data) === false){
	        $this->error = '添加数据失败';
	        return false;
	    }
	    return true;
	}
    
	/**
	 * @todo 删除展馆品类
	 * @author sap
	 * @since 2016-3-10
	 */
	public function zoneCategoryDel(){
	    $zone_id = (int)I('zone_id');
	    $online_category_id = (int)I('online_category_id');
	    $activity_id = (int)I('activity_id');
	    if($online_category_id == 0 || $zone_id == 0 || $activity_id == 0){
	        $this->error = '活动、展馆或品类不正确';
	        return false;
	    }
        $map = array();
        $map['zone_id'] = $zone_id;
        $map['online_category_id'] = $online_category_id;
        $map['activity_id'] = $activity_id;
        if(M('OnlineZoneCategory')->where($map)->delete() === false){
            $this->error = '删除失败';
            return false;
        }
        return true;
	}
	
	/**
	 * @todo OnlineZoneCategory表 - 数据查询
	 * @author sap
	 * @since 2016-3-11
	 */
	public function onlineZoneCategoryList($zone_id='',$online_category_id=''){
	    $map = array();
	    if(!empty($zone_id)){
	        $map['zone_id'] = array('in',$zone_id);
	    }
	    if(!empty($online_category_id)){
	        $map['online_category_id'] = array('in',$online_category_id);
	    }
	    if(empty($map)){
            return null;
	    }
	    return M('OnlineZoneCategory')->where($map)->field('zone_id,online_category_id,activity_id')->select();
	}
	
	/**
	 * @todo 返回最后一次错误信息
	 * @return string
	 * @author PHP@妖孽 <dino_ma@163.com>
	 */
	public function getError(){
		return $this->error;
	}

}
